/****************************************************************************

 Copyright (C) 2009 Jean Pierre Charalambos. All rights reserved.

 Jean Pierre Charalambos (http://dis.unal.edu.co/profesores/pierre/) is an
 Affiliate Associate Professor of the Faculty of Engineering at National
 University of Colombia (http://www.unal.edu.co/).

 This file is part of maku version 0.2.

 http://code.google.com/p/maku/
 email: jpcharalambosh@unal.edu.co

 This file may be used under the terms of the GNU General Public License
 versions 2.0 or 3.0 as published by the Free Software Foundation and
 appearing in the COPYING file included in the packaging of this file.

 This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

*****************************************************************************/

#include <QtOpenGL>
#include "mainwindow.h"

MainWindow::MainWindow ( QWidget *parent ) : QMainWindow ( parent ) {
    ui.setupUi ( this );
    //QMetaObject::connectSlotsByName(this); //weird: doesn't work, since slots get called twice!
    connect ( ui.openAction, SIGNAL ( triggered() ), this, SLOT ( open() ) );
    connect ( ui.aboutAction, SIGNAL ( triggered() ), this, SLOT ( about() ) );
    connect ( ui.exitAction, SIGNAL ( triggered() ), this, SLOT ( close() ) );
    init();
}

void MainWindow::init() {
    setAttribute ( Qt::WA_DeleteOnClose );

    simpleScene = new SimpleScene();

    view = new QGraphicsView ( simpleScene, this );

    //si la plataforma soporta OpenGL entonces lo usa
    if ( QGLFormat::hasOpenGL() ) {
        QGLFormat format;
        format.setDepth ( true );
        //format.setStencil(false); // not por ahora...
        format.setDoubleBuffer ( true ); //acelarar animaciones
        format.setSampleBuffers ( true ); //se necesita para hacer antialiasing en OpenGL
        view->setViewport ( new QGLWidget ( format ) );
    }

    view->setRenderHint ( QPainter::Antialiasing );
    view->setObjectName ( QString::fromUtf8 ( "view" ) );

    setCentralWidget ( view );

    statusBar()->showMessage ( tr ( "Ready" ) );

    readSettings();

    setWindowTitle ( tr ( "Primitives-2D" ) );

    importActions();
}

void MainWindow::importActions() {
    ui.sceneMenu->addAction ( simpleScene->lineColorAction );
}

void MainWindow::open() {
    QString fileName = QFileDialog::getOpenFileName ( this, tr ( "Choose a model to open" ) );
    //here goes code to open model...
    statusBar()->showMessage ( tr ( "File loaded" ), 2000 );
}

void MainWindow::about() {
    QMessageBox::about ( this, tr ( "About Application" ),
                         tr ( "An experimental <b>Application</b> that supports "
                              "advanced 2D rendering techniques." ) );
}

void MainWindow::closeEvent ( QCloseEvent *event ) {
    if ( maybeSave() ) {
        writeSettings();
        event->accept();
    } else {
        event->ignore();
    }
}

void MainWindow::writeSettings() {
    QSettings settings ( tr ( "National University of Colombia" ), tr ( "2D scene viewer" ) );
    settings.setValue ( tr ( "pos" ), pos() );
    settings.setValue ( tr ( "size" ), size() );
}

void MainWindow::readSettings() {
    QSettings settings ( tr ( "National University of Colombia" ), tr ( "2D scene viewer" ) );
    QPoint pos = settings.value ( tr ( "pos" ), QPoint ( 200, 200 ) ).toPoint();
    QSize size = settings.value ( tr ( "size" ), QSize ( 400, 400 ) ).toSize();
    move ( pos );
    resize ( size );
}
